﻿using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Development.Tools
{
    public class XmlCollectionSerializable
    {
        public static StringBuilder XmlSerializerDic<K,V>(Dictionary<K,V> dic)
        {
            var keySerializer = new XmlSerializer(typeof(K));
            var valueSerializer = new XmlSerializer(typeof(V));

            StringBuilder sBulder = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.ConformanceLevel = ConformanceLevel.Auto;
            XmlWriter writer = XmlWriter.Create(sBulder, settings);
            foreach (K key in dic.Keys)
            {
                writer.WriteStartElement("item");
                writer.WriteStartElement("key");
                keySerializer.Serialize(writer, key);
                writer.WriteEndElement();
                writer.WriteStartElement("value");
                V value = dic[key];
                valueSerializer.Serialize(writer, value);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();
                sBulder.Append("\r\n");
            }
            writer.Close();
            return sBulder;
        }

        public static void XmlDeserializeDic<K, V>(string xml, Dictionary<K, V> dic)
        {
            var keySerializer = new XmlSerializer(typeof(K));
            var valueSerializer = new XmlSerializer(typeof(V));

            byte[] buffer = Encoding.UTF8.GetBytes(xml);
            MemoryStream stream = new MemoryStream(buffer);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Auto;

            XmlReader reader = XmlReader.Create(stream, settings);
            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();

            if (wasEmpty)
                return;
            while (reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.None)
            {
                reader.ReadStartElement("item");
                reader.ReadStartElement("key");
                var key = (K)keySerializer.Deserialize(reader);
                reader.ReadEndElement();
                reader.ReadStartElement("value");
                var value = (V)valueSerializer.Deserialize(reader);
                reader.ReadEndElement();
                dic.Add(key, value);
                reader.ReadEndElement();
                reader.MoveToContent();
            }
        }  


        public StringBuilder XmlSerializerList<T>(List<T> list)
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<T>));
            StringBuilder sBulder = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.ConformanceLevel = ConformanceLevel.Auto;
            XmlWriter writer = XmlWriter.Create(sBulder, settings);
            ser.Serialize(writer, list);
            writer.Close();
            return sBulder;
        }

        public static List<T> XmlDeserializeList<T>(string xml)
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<T>));
            byte[] buffer = Encoding.UTF8.GetBytes(xml);
            MemoryStream stream = new MemoryStream(buffer);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Auto;
            XmlReader reader = XmlReader.Create(stream, settings);
            return ser.Deserialize(stream) as List<T>;
        }
    }
}
